<?php
/**
 * Created by PhpStorm.
 * User: zengyuming
 * Date: 2017/8/21
 * Time: 下午4:51
 */


namespace Admin\Controller;
class StatisticsController extends CommonController {
    private $StartTime = "";
    private $EndTime = "";

    public function _initialize(){
        // 本周  7天内
        $now = time();
        $time = '1' == date('w') ? strtotime('Monday', $now) : strtotime('last Monday', $now);
        $this->StartTime = strtotime(date('Y-m-d 00:00:00', $time));
        $this->EndTime = strtotime(date('Y-m-d 23:59:59', strtotime('Sunday', $now)));
    }

    /* 项目支付占比 */
    public function ProjectPaymentroportion(){
        $this->assign('current_Titlename' , '项目统计');
        $where = array(
            'a.pay_status' => 1,
            'a.update_begtime' => array(array("EGT" ,  $this->StartTime), array('ELT',$this->EndTime))
        );
        $data = M("transaction")
            ->alias("a")
            ->field('b.Userremarks , sum(paymoney) as peymoney , merchant , (select sum(paymoney) as peymoney from zym_transaction as a left join zym_user as b on a.merchant = b.merchant_id where a.update_begtime > '. $this->StartTime .' and a.update_begtime < '. $this->EndTime .') as sumpaymoney')
            ->join('left join zym_user as b on a.merchant = b.merchant_id')
            ->where($where)
            ->group('a.merchant')
            ->select();
        foreach ($data as $key => $val){
           $Percentage = sprintf("%.1f",($val['peymoney'] / $val['sumpaymoney']) * 100);
           if($Percentage <= 0.1) continue;
           $narr[] = array(
               'userremarks' => $val['userremarks'],
               'Percentage' => $Percentage,
           );
           unset($data[$key]);
        }
        $this->assign('Percentage_data' , json_encode($narr));
        $this->display('ProjectPaymentroportion');
    }

    /* 支付成功率 */
    public function PaySuccessRate(){
        $this->assign('current_Titlename' , '支付成功率');
        $Model = new \Admin\Model\transactionModel();
        $res = $Model->successrate();
//        $SuccessRateConf = array();
//        foreach($res as $key => $val){
//            $sumpen = $val['fail_num'] + $val['success_num'];
//            $rate = ($val['success_num'] / $sumpen) * 100;
//            $SuccessRateConf[] = array(
//                'rate' => $rate,
//                'passagewayName' => $val['asname']
//            );
//        }
        $this->assign('successrate_data' , json_encode($res));
        $this->display('Paysuccessrate');
    }

}